using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_ModifyUser : Controls.DataManagerUserControl
{
    protected override void ItemChanged()
    {
        if (CurrentUser != null)
        {
            Refresh();
        }
        else
        {
            ClearFields();
        }
    }

    private void Refresh()
    {
        txtUsername.Text = CurrentUser.Username;
        lblUsername.Text = Server.HtmlEncode(CurrentUser.Username);
        txtBio.Text = CurrentUser.Bio;
        txtEmail.Text = CurrentUser.EmailAddress;
        txtInterests.Text = CurrentUser.Interests;
        txtLocation.Text = CurrentUser.Location;
        txtOccupation.Text = CurrentUser.Occupation;
        txtSignature.Text = CurrentUser.Signature;
        txtWebsite.Text = CurrentUser.Website;

        chkAllowAvatar.Checked = CurrentUser.AdminSettings.AllowAvatar;
        chkAllowSignature.Checked = CurrentUser.AdminSettings.AllowSignature;
        chkAllowUserTitle.Checked = CurrentUser.AdminSettings.AllowUserTitle;
        chkAllowPublicAdmin.Checked = CurrentUser.AdminSettings.AllowPublicAdmin;

        if (Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator))
        {
            pnlUserlevel.Visible = true;
            switch (CurrentUser.Status)
            {
                case DAL.User.UserStatus.Administrator:
                    ddlUserLevel.SelectedValue = "4";
                    break;
                case DAL.User.UserStatus.SuperModerator:
                    ddlUserLevel.SelectedValue = "3";
                    break;
                case DAL.User.UserStatus.Moderator:
                    ddlUserLevel.SelectedValue = "2";
                    break;
                case DAL.User.UserStatus.Banned:
                    ddlUserLevel.SelectedValue = "-1";
                    break;
                default:
                    ddlUserLevel.SelectedValue = "1";
                    break;
            }
        }
        else
        {
            pnlUserlevel.Visible = false;
        }

        if (CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            btnBan.Visible = false;
        }
        else
        {
            btnBan.Visible = true;
        }
    }

    private void ClearFields()
    {
        txtPassword1.Text = "";
        txtPassword2.Text = "";

        txtUsername.Text = "";
        lblUsername.Text = "";
        txtBio.Text = "";
        txtEmail.Text = "";
        txtInterests.Text = "";
        txtOccupation.Text = "";
        txtSignature.Text = "";
        txtWebsite.Text = "";
        
        chkAllowAvatar.Checked = false;
        chkAllowSignature.Checked = false;
        chkAllowUserTitle.Checked = false;
        chkAllowPublicAdmin.Checked = false;
        ddlUserLevel.SelectedValue = "0";
    }
    protected void btnUpdatePassword_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && !String.IsNullOrEmpty(txtPassword1.Text) &&
            (!CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator) || Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator)))
        {
            DAL.Users.ChangePassword(CurrentUser, txtPassword1.Text);
            pnlPasswordChanged.Visible = true;
        }
    }
    protected void CheckUsername(object source, ServerValidateEventArgs args)
    {
        if (CurrentUser.Username.Equals(txtUsername.Text, StringComparison.OrdinalIgnoreCase))
        {
            args.IsValid = true;
        }
        else
        {
            if (DAL.Users.GetByUsername(txtUsername.Text) != null)
            {
                // user with the same name already exists
                args.IsValid = false;
            }
            else
            {
                // username is taken
                args.IsValid = true;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && 
            (!CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator) ||
            Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator)))
        {
            CurrentUser.Username = txtUsername.Text;
            CurrentUser.Bio = txtBio.Text;
            CurrentUser.EmailAddress = txtEmail.Text;
            CurrentUser.Interests = txtInterests.Text;
            CurrentUser.Location = txtLocation.Text;
            CurrentUser.Occupation = txtOccupation.Text;
            CurrentUser.Signature = txtSignature.Text;
            CurrentUser.Website = txtWebsite.Text;

            CurrentUser.AdminSettings.AllowAvatar = chkAllowAvatar.Checked;
            CurrentUser.AdminSettings.AllowSignature = chkAllowSignature.Checked;
            CurrentUser.AdminSettings.AllowUserTitle = chkAllowUserTitle.Checked;
            CurrentItem.AdminSettings.AllowPublicAdmin = chkAllowPublicAdmin.Checked;
            
            if (Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator))
            {
                switch (ddlUserLevel.SelectedValue)
                {
                    case "4":
                        CurrentUser.Status = DAL.User.UserStatus.Administrator;
                        break;
                    case "3":
                        CurrentUser.Status = DAL.User.UserStatus.SuperModerator;
                        break;
                    case "2":
                        CurrentUser.Status = DAL.User.UserStatus.Moderator;
                        break;
                    case "-1":
                        DAL.Users.BanUser(CurrentUser);
                        break;
                    case "1":
                        CurrentUser.Status = DAL.User.UserStatus.Member;
                        break;
                }
            }

            DAL.Users.Save(CurrentUser);
            pnlProfileSaved.Visible = true;
        }
        Refresh();
    }

    private void BanUser()
    {
        DAL.Users.BanUser(CurrentUser);
        Refresh();
    }

    protected void btnAnnihilate_Click(object sender, EventArgs e)
    {
        if (!CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            DAL.Users.AnnihilateSpammer(CurrentUser);
            lblAnnihilate.Visible = true;
        }
    }
    protected void btnBan_Click(object sender, EventArgs e)
    {
        if (!CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            BanUser();
        }
    }
}